package com.yjt.gcss.gddw.common.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.jdbc.Work;
import org.springframework.stereotype.Service;

import com.yjt.common.easyui.datagrid.DataGridDAOImpl;
import com.yjt.common.hibernate.base.AbstractDaoImpl;
import com.yjt.common.hibernate.base.DaoException;
import com.yjt.gcss.gddw.common.bean.domain.UiPlds;

@Service(UiPldsDao.BEAN_PACKAGE_NAME)
public class UiPldsDao extends DataGridDAOImpl {

	public static final String BEAN_PACKAGE_NAME = "com.yjt.gcss.gddw.common.dao.UiPldsDao";
	public UiPldsDao(){

	}

	public UiPlds getBeanById(String id) throws DaoException {
		return (UiPlds) getBeanById(UiPlds.BEAN_PACKAGE_NAME, id, false);
	}
	
	public UiPlds getBeanByIdForUpdate(String id) throws DaoException {
		return (UiPlds) getBeanById(UiPlds.BEAN_PACKAGE_NAME, id, true);
	}

	@SuppressWarnings("rawtypes")
	public Collection getAll() throws DaoException {
		return getAll(UiPlds.BEAN_PACKAGE_NAME);
	}

	@SuppressWarnings("rawtypes")
	public UiPlds getBeanByParams(String hqlName,ArrayList paraList) throws DaoException{
		return (UiPlds) getProtoBeanByParams(hqlName,paraList);
	}

	public UiPlds getBeanByBean(UiPlds bean, MatchMode mode) throws DaoException{
		return (UiPlds) getProtoBeanByBean(bean, mode);
	}
	
	public UiPldsDao setOrder(Order order){
		return (UiPldsDao)super.addOrder(order);
	}
	
	public ArrayList<String> getSequenceByParma(final String sequenceName, final String incrementCount) {
		Session session = getSessionFactory().getCurrentSession();
        final ArrayList<String> retValues = new ArrayList<String>();   
		session.doWork(new Work() {
            @Override  
            public void execute(Connection conn) throws SQLException   
            {   
                CallableStatement proc = null;   
                try  
                {   
                    proc = conn.prepareCall("{CALL SPROC_GENERATE_SEQ(?,?,?,?)}");   
                    proc.setString(1, sequenceName);
                    proc.setInt(2, Integer.parseInt(incrementCount));
                    proc.registerOutParameter(3, java.sql.Types.VARCHAR);   
                    proc.registerOutParameter(4, java.sql.Types.VARCHAR);   
                    proc.execute();   
                    retValues.add(proc.getString(3));   
                    retValues.add(proc.getString(4));   
                }   
                catch(Exception e)
                {   
                    throw new DaoException(e);
                }   
                finally  
                {   
                    if(null != proc)   
                    {   
                        proc.close();
                        proc = null;
                    }   
                }   
            }   
        });
		return retValues;
	}
}
